package samsung.sds.survey.surveyManagement.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.survey.surveyManagement.model.QuestionEntity;
import samsung.sds.survey.surveyManagement.model.QuestionModel;
import samsung.sds.survey.surveyManagement.model.SurveyEntity;
import samsung.sds.survey.surveyManagement.service.SurveyDesignService;
import samsung.sds.system.utils.Constants;
/**
 * @author sp.deng
 * @createDate 2016-01-13
 * 
 * 投票管理功能模块的问题编辑操作控制器
 */
@Controller
@RequestMapping("/survey/surveyManagement/surveyDesign")
public class SurveyDesignController  {
	
	@Autowired 
	SurveyDesignService surveyDesignServiceImpl;
	
	/**
	 * 显示问题编辑的主页面
	 * @param surveyId 问卷id
	 * @param map 用于封装往页面传递的对象
	 * @return
	 */
	@RequestMapping(value="/gotoEditQuestion",method=RequestMethod.POST)
	public String gotoEditQuestion(Map<String, Object> map,String surveyId){
		SurveyEntity survey = surveyDesignServiceImpl.findEntityById(surveyId.toString());
		List<QuestionEntity> questions = surveyDesignServiceImpl.getQuestionEntity(surveyId);
		map.put("survey", survey);
		map.put("questions", questions); 
		return "/survey/surveyManagement/surveyDesign";
	}
	
	/**
	 * 保存修改项
	 * @param surveyEntity 问卷实体
	 * @param qlist 问题模型 
	 * @return
	 */
	@RequestMapping(value="/saveQuestion",method= RequestMethod.POST)
	@ResponseBody
	public String saveQuestion(SurveyEntity surveyEntity, QuestionModel qlist) {

		boolean flag = surveyDesignServiceImpl.saveQuestion(surveyEntity, qlist);

		if (flag) {// 保存成功
			return Constants.SUCCESS_CODE;
		} else {// 保存失败
			return Constants.FAILURE_CODE;
		}
	}
}
